package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Pharmacy;


@Repository(value = "pharmacyDao")
@Transactional(readOnly = false)
public class PharmacyDaoImpl implements PharmacyDao {
	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	} 
	
	public List<Pharmacy> getListPharmacy() {
		TypedQuery<Pharmacy> q = em.createNamedQuery("Pharmacy.findAll",Pharmacy.class);
    	return q.getResultList();
	}
	public List<Pharmacy> getListPharmacy(boolean active) {
		TypedQuery<Pharmacy> q = em.createNamedQuery("Pharmacy.findByActive",Pharmacy.class);
    	q.setParameter("active",active);
    	return q.getResultList();
	}
	
	/*public List<Pharmacy> getListPharmacyByPharmacyIdByActive(Integer pharmacyId,boolean active) {
		TypedQuery<Pharmacy> q = em.createNamedQuery("Pharmacy.findByPharmacyIdByActive",Pharmacy.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}*/
	
	/*Return Pharmacy*/
	public List<Pharmacy> getListPharmacyOne(Integer pharmacyId){
		TypedQuery<Pharmacy> q = em.createNamedQuery("Pharmacy.findByPharmacyId",Pharmacy.class);
		q.setParameter("pharmacyId", pharmacyId);
		return q.getResultList();
	}	
	
	public List<Pharmacy> getListPharmacyByPhaPharmacyIdByActive(Integer pharmacyId,boolean active) {
		TypedQuery<Pharmacy> q = em.createNamedQuery("Pharmacy.findByPhaPharmacyIdByActive",Pharmacy.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}
	
	
	public List<Pharmacy> getListPharmacy(Integer regionId) {
		TypedQuery<Pharmacy> q = em.createNamedQuery("Pharmacy.findByRegionId",Pharmacy.class);
    	q.setParameter("regionId",regionId);
    	return q.getResultList();
	}
	public List<Pharmacy> getListPharmacy(Integer regionId,boolean active) {
		TypedQuery<Pharmacy> q = em.createNamedQuery("Pharmacy.findByRegionIdByActive",Pharmacy.class);
    	q.setParameter("active",active);
    	q.setParameter("regionId",regionId);
    	return q.getResultList();
	}
	public Pharmacy findByPharmacyId(Integer pharmacyId) {
		Pharmacy res=null;
		try {
			res=em.find(Pharmacy.class,pharmacyId);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return res;
	}
	public void update(Pharmacy pharmacy) {
		em.merge(pharmacy);
	}
	public void remove(Pharmacy pharmacy) {
		em.remove(em.getReference(Pharmacy.class,pharmacy.getPhapharmacyId()));
	}
	public void add(Pharmacy pharmacy) {
		em.persist(pharmacy);
	}
}
